<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends Admin_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->library('Form_validation');
		$this->load->model('User_model');
		$this->load->helper('url');
		$this->lang->load('admin/user');
		$this->lang->load('admin/common');
		$this->load->library('hash');
	}
	/*---------------------Bat dau Xem tat ca cac ban ghi--------------------------*/
    public function getAll()
	{
		/*------------------------------------------------------------------------------*/
		if(isset($_POST['page']))
			$page =$_POST['page'] ; // get the requested page
		else $page=1;
		if(isset($_POST['rows']))
			$limit=$_POST['rows'];// get how many rows we want to have into the grid
		else $limit = 1;
		if(isset($_POST['sidx']))
			$sidx=$_POST['sidx'];// get index row - i.e. user click to sort
		else $sidx = 'use_id';
		if(!$sidx) $sidx ='use_id';
		if(isset($_POST['sord']))
			$sord=$_POST['sord'];// get the direction
		else $sord= 'desc';
		$total_pages=0;
		$where='';
		$by=$sord;
		$order=$sidx;
		//echo 'sap xep:'.$sidx;
		//echo 'thu thu:'.$sord;
		
		$count = count($this->User_model->fetch("use_id", $where, "", ""));
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else { $total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$select = "use_id, use_username, use_fullname, use_email, use_address, use_phone, use_yahoo, use_status, use_regisdate, use_enddate";
		$result = $this->User_model->fetch($select, $where, $sidx, $sord, $start, $limit);
		 
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;
		foreach($result as $userArray)
		{
			$responce->rows[$i]['id']=$userArray->use_id;
			$responce->rows[$i]['cell']=array($userArray->use_id,$userArray->use_username,$userArray->use_fullname,$userArray->use_email,$userArray->use_address,$userArray->use_status,$userArray->use_yahoo);
			$i++;
		}
		echo json_encode($responce);
	}
	/*---------------------Ket thuc Xem tat ca cac ban ghi--------------------------*/
	
	/*---------------------Bat dau Xem 1 ban ghi--------------------------*/
    //Lay thong tin 1 thanh vien
	public function getOneMember()
	{
		if(isset($_POST['id']))
			$use_id =$_POST['id'] ; // get use_id
		else $use_id=-1;
		if(isset($_POST['member']))
			$use_username=$_POST['member'];// get use_userName
		else $use_username ='-1';
		$start=0;
		$limit=1;
		$sidx='';
		$sord='';
		$where='use_id='.$use_id." and use_username='".$use_username."'";
		$select = "use_id, use_username, use_fullname, use_email, use_address,use_birthday, use_phone,use_mobile, use_yahoo,use_skype,use_province,use_sex, use_status, use_regisdate, use_enddate";
		$result = $this->User_model->fetch($select, $where, $sidx, $sord, $start, $limit);
		$i=0;
		foreach($result as $userArray)
		{
			$responce->rows[$i]['id']=$userArray->use_id;
			$responce->rows[$i]['name']=$userArray->use_username;
			$responce->rows[$i]['fullname']=$userArray->use_fullname;
			$responce->rows[$i]['email']=$userArray->use_email;
			$responce->rows[$i]['addr']=$userArray->use_address;
			$responce->rows[$i]['phone']=$userArray->use_phone;
			$responce->rows[$i]['mobil']=$userArray->use_mobile;
			$responce->rows[$i]['birthday']=date("d/m/Y",$userArray->use_birthday);
			$responce->rows[$i]['yahoo']=$userArray->use_yahoo;
			$responce->rows[$i]['skype']=$userArray->use_skype;
			$responce->rows[$i]['gender']=$userArray->use_sex;
			$responce->rows[$i]['province']=$userArray->use_province;
			$i++;			
		}
		
		echo json_encode($responce);
	}
	/*---------------------Ket thuc Xem 1 ban ghi--------------------------*/
	
	
	public function register()
	{
		/*
		$this->form_validation->set_rules('userName','UserName','trim|required|alpha_numeric|min_length[6]|callback_user_not_exites');
		if($this->form_validation->run()==false){
			$this->load->view('view_register');
		}else
		{
			$userName=$this->input->post('userName');
			$this->User_model->register_user($userName,$userName);
			
		}	
		*/
		$this->load->view('admin/user/view_user');		
	}
	private function add()
	{
		$dates  =trim($this->filter->injection_html($this->input->post('birthday_edit')));
		$birthday = explode("/", $dates);
		$_day=$birthday[0];
		$_month=$birthday[1];
		$_year=$birthday[2];
		$regisdate = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
		
		$salt = $this->hash->key(8);
		$dataAdd = array(
				'use_username'      =>      trim(strtolower($this->filter->injection_html($this->input->post('userName_edit')))),
				'use_password'      =>      $this->hash->create($this->input->post('password_edit'), $salt, 'md5sha512'),
				'use_salt'          =>      $salt,
				'use_email'         =>      trim(strtolower($this->filter->injection_html($this->input->post('email_edit')))),
				'use_fullname'      =>      trim($this->filter->injection_html($this->input->post('fullName_edit'))),
				'use_birthday'      =>      mktime(0, 0, 0, (int)$_month, (int)$_day, (int)$_year),
				'use_sex'           =>      (int)$this->input->post('gender_edit'),
				'use_address'       =>      trim($this->filter->injection_html($this->input->post('address_edit'))),
				'use_province'      =>      (int)$this->input->post('city_edit'),
				'use_phone'         =>      trim($this->filter->injection_html($this->input->post('tel_edit'))),
				'use_mobile'        =>      trim($this->filter->injection_html($this->input->post('mobil_edit'))),
				'use_yahoo'         =>      trim($this->filter->injection_html($this->input->post('yahoo_edit'))),
				'use_skype'         =>      trim($this->filter->injection_html($this->input->post('sky_edit'))),
				'use_group'         =>      '1',
				'use_status'        =>      '1',
				'use_regisdate'     =>      $regisdate,
				'use_enddate'       =>      '',
				'use_key'           =>      $this->hash->create($this->input->post('userName_edit'), $this->input->post('email_edit'), 'sha256md5'),
				'use_lastest_login' =>      ''
		);
		 if($this->User_model->add($dataAdd))
		 {
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		 }else
		 {
		 	$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		 }
		   
	}
   private function edit()
	{
		
		$dates  =trim($this->filter->injection_html($this->input->post('birthday_edit')));
		$id=$this->filter->injection_html($this->input->post('id_edit'));
		$username=trim(strtolower($this->filter->injection_html($this->input->post('userName_edit'))));
		$birthday = explode("/", $dates);
		$_day=$birthday[0];
		$_month=$birthday[1];
		$_year=$birthday[2];
		$this->load->library('hash');
		$salt = $this->hash->key(8);
		$dataEdit = array(
				//'use_username'      =>      trim(strtolower($this->filter->injection_html($this->input->post('userName_edit')))),
				//'use_password'      =>      $this->hash->create($this->input->post('password_edit'), $salt, 'md5sha512'),
				//'use_salt'          =>      $salt,
				//'use_email'         =>      trim(strtolower($this->filter->injection_html($this->input->post('email_edit')))),
				'use_fullname'      =>      trim($this->filter->injection_html($this->input->post('fullName_edit'))),
				'use_birthday'      =>      mktime(0, 0, 0, (int)$_month, (int)$_day, (int)$_year),
				'use_sex'           =>      (int)$this->input->post('gender_edit'),
				'use_address'       =>      trim($this->filter->injection_html($this->input->post('address_edit'))),
				'use_province'      =>      (int)$this->input->post('city_edit'),
				'use_phone'         =>      trim($this->filter->injection_html($this->input->post('tel_edit'))),
				'use_mobile'        =>      trim($this->filter->injection_html($this->input->post('mobil_edit'))),
				'use_yahoo'         =>      trim($this->filter->injection_html($this->input->post('yahoo_edit'))),
				'use_skype'         =>      trim($this->filter->injection_html($this->input->post('sky_edit'))),
				//'use_group'         =>      '1',
				//'use_status'        =>      '1'
				//'use_regisdate'     =>      '',
				//'use_enddate'       =>      '',
				//'use_key'           =>      $this->hash->create($this->input->post('userName_edit'), $this->input->post('email_edit'), 'sha256md5'),
				//'use_lastest_login' =>      ''
		);
		  
		
		if($this->User_model->update($dataEdit, "use_id = ".(int)$id." and use_username='".$username."'"))
		{
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		 }else
		 {
		 	$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		 }
			
	}
   private function del()
	{
		$id=$this->filter->injection_html($this->input->post('id_edit'));
		if($this->User_model->delete($id, "use_id"))
		{
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		}else
		{
			$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		}
		
	}
    private function active()
	{
		echo $_POST['userName'];
	}
	private function deActive()
	{
		echo $_POST['userName'];
	}
	/*-----------------------Bat dau xu ly thong tin: them sua xoa...-------------*/
	public function process()
	{
		$act=$this->filter->injection_html($this->input->post('act'));
		switch ($act) 
		{
		    case "add"://add
		        $this->add();
		        break;
		    case "edit"://edit
		        $this->edit();
		        break;
		    case "del"://delete
		        $this->del();
		        break;
		     case "active"://kich hoat
		        $this->active();
		        break;
		     case "deActive":
		        $this->deActive();
		        break;
		     default:
		     	echo 'No process';
		}
	}
	/*-----------------------Ket thuc xu ly thong tin: them sua xoa...-------------*/
	public function index()
	{
		$this->load->view('admin/user/view_user');
		//$this->register();
	}
	public function user_not_exites($userName)
	{
		$this->form_validation->set_message('user_not_exites','Da ton tai');
		if($this->User_model->check_user($userName))
		{
			return false;
		}else
			return true;
	}
	
}

